package com.xxrl.shop.service;

import com.xxrl.shop.domain.Merchant;
import com.xxrl.shop.domain.User;
import com.xxrl.shop.repository.MerchantRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * @author mis
 */
@Service
public class MerchantService {

    private final MerchantRepository merchantRepository;

    @Autowired
    public MerchantService(MerchantRepository merchantRepository) {
        this.merchantRepository = merchantRepository;
    }

    public Page<Merchant> findAll(PageRequest pageRequest) {
        return merchantRepository.findAll(pageRequest);
    }

    public void addMerchantByUser(User user, Merchant merchant) {
        merchant.setUser(user);
        merchant.setContent("default");
        merchant.setId(null);
        merchantRepository.save(merchant);
    }

    public Merchant getMerchantByUser(User user) {
        return merchantRepository.queryByUser(user);
    }

    public void saveMerchant(Merchant merchant) {
        merchantRepository.save(merchant);
    }
}
